<?php
declare (strict_types = 1);

namespace app\model\communal;

use think\db\Query;
use think\Model;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;

/**
 * Class app\model\communal\UserExpertEvaluateModel
 */
class UserExpertEvaluateModel extends Model
{
    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'communal';

    /**
     * 主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 表名
     *
     * @var string
     */
    protected $table = '55300_user_expert_evaluate';

    /**
     * 自动时间戳
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间字段设置
     *
     * @var string
     */
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    /**
     * 软删除
     */
    use SoftDelete;
    protected $deleteTime = 'delete_at';
    protected $defaultSoftDelete = 0;

    /**
     * 展示字段名
     *
     * @var string[]
     */
    public static $column = ['user_id', 'expert_id', 'satisfy_content', 'content', 'stars', 'is_incognito', 'state', 'created_at'];

    /**
     * 搜索字段名
     *
     * @var string[]
     */
    public static $searchColumn = [
        'id', 'user_id', 'expert_id', 'content', 'stars', 'state',

        'satisfy_contents',
    ];

    /**
     * 新增字段
     *
     * @var string[]
     */
    public static $saveColumn = ['user_id', 'expert_id', 'satisfy_content', 'content', 'stars', 'is_incognito'];

    /**
     * 满意内容 选项
     */
    const SATISFY_OPTION_ONE = 1; // 效果很明显
    const SATISFY_OPTION_TWO = 2; // 态度很好
    const SATISFY_OPTION_THREE = 3; // 一般

    /**
     * 满意内容选项 Options
     */
    public static $satisfyOptions = [
        ['id' => self::SATISFY_OPTION_ONE, 'name' => '效果很明显'],
        ['id' => self::SATISFY_OPTION_TWO, 'name' => '态度很好'],
        ['id' => self::SATISFY_OPTION_THREE, 'name' => '一般'],
    ];

    /**
     * expert_id 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchExpertIdAttr($query, $value)
    {
        if (!empty($value)) {
            $query->where('expert_id', $value);
        }
    }

    /**
     * satisfy_content 满意内容搜索 待优化
     *
     * @param Query $query
     * @param $value
     */
    public function searchSatisfyContentAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $query->where('satisfy_content', 'like', '%' . $value . '%');
        }
    }

    /**
     * satisfy_contents &&搜索 待优化
     *
     * @param Query $query
     * @param $value
     */
    public function searchSatisfyContentsAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $value = explode(',', (string)$value);
            $query->where(function (Query $query) use ($value) {
                foreach ($value as $item) {
                    $query->where('satisfy_content', 'like', '%' . $item . '%');
                }
            });
        }
    }

    /**
     * 关联用户
     *
     * @return BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(UserModel::class, 'user_id', 'id')
            ->removeOption('soft_delete')
            ->bind([
                'user_name' => 'name',
                'user_avatar' => 'avatar',
                'user_mobile' => 'mobile'
            ]);
    }
}
